package edu.gatech.app4args;

import edu.gatech.app4args.utils.CompendiumNode;
import edu.gatech.app4args.utils.CustomListModel;
import edu.gatech.app4args.utils.NodeDropHandler;

import java.awt.Dimension;

import java.util.ArrayList;

import javax.swing.DropMode;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.TransferHandler;

import org.netbeans.lib.awtextra.AbsoluteConstraints;
import org.netbeans.lib.awtextra.AbsoluteLayout;

/**
 * A representation of a single section of slide content.
 *
 * @author Andrew Lunsford
 */
public class SlideContentSection extends JPanel{
	/** The section label */
	private JLabel label;
	/** The list that shows the current data in the section */
	private JList list;
	/** The scroll pane that allows the list to scroll */
	private JScrollPane scrollpane;
	/** The section number for this instance */
	private int sectionNumber;
	/** A reference to the panel that created this section */
	private static SlideContentPanel slideContentPanel;

	/**
	 * Creates a new SlideContentSection, setting its layout and initializing
	 * all objects in the section.
	 *
	 * @param scp a reference to the parent panel
	 * @param sectionNumber the number of the section
	 */
	public SlideContentSection(SlideContentPanel scp, int sectionNumber){
		this.setName("SCS "+sectionNumber);
		slideContentPanel = scp;
		this.sectionNumber = sectionNumber;
		this.setLayout(new AbsoluteLayout());

		label = new JLabel();
		label.setName("SCS "+sectionNumber+" Label");

		list = new JList();
		list.setName("Content Section "+sectionNumber+" List");
		list.setModel(new CustomListModel(this));

		list.setDropMode(DropMode.ON);
		list.setTransferHandler(new NodeDropHandler(TransferHandler.COPY));

		scrollpane = new JScrollPane(list);
		scrollpane.setName("SCS "+sectionNumber+" Scrollpane");
	}

	/**
	 * Sets up the section, changing the name, adjusting the size of the scroll
	 * pane and maximum size according to the number of rows in the list.
	 *
	 * @param name the name of the section
	 * @param rows the number of rows the list should display at a time
	 */
	public void setup(String name, int rows){
                label.setText(name);
		this.add(label, new AbsoluteConstraints(20, 10, -1, -1));
		// TODO decrease height to accomodate horizontal scrollbar
                this.add(scrollpane, new AbsoluteConstraints(14, 30, 410, rows*25));
            	this.setMaximumSize(new Dimension(450, rows*20+45));
	}

	public JLabel getLabel(){
		return label;
	}
	/**
	 * Loads the list with the given content.
	 *
	 * @param data the content to load into the list.
	 */
	public void load(ArrayList<String> data){
		((CustomListModel)list.getModel()).removeAllElements();
		for(String str : data){
			((CustomListModel)list.getModel()).addElement(str);
		}
	}

	/**
	 * Makes a call to the parent panel to add content to the current slide
	 * and section.
	 *
	 * @param key the id of the CompendiumNode to add content from
	 * @return true if the content was added successfully
	 */
	public boolean addSlideData(String key){
		System.out.println("SCS> adding node: "+key);
		return slideContentPanel.addSlideData(sectionNumber, key);
	}

	/**
	 * Calls getNode in the parent panel to retrieve a CompendiumNode.
	 *
	 * @param key the id of the CompendiumNode to get
	 * @return the requested CompendiumNode
	 */
	public CompendiumNode getNode(String key){
		return slideContentPanel.getNode(key);
	}

	/**
	 * Gets the list in the section.
	 *
	 * @return the list for the section
	 */
	public JList getList(){
		return list;
	}

	/**
	 * Prints the data for all objects in the section.
	 */
	public void printString(){
		String out = "section       : "+this+"\n";
		out += "label         : "+label.getText()+"\n";
		out += "list          : "+((CustomListModel)list.getModel()).elements()+"\n";
		out += "scrollpane    : "+scrollpane+"\n";
		out += "scrollpane VR : "+scrollpane.getViewport().getView().getName();
		System.out.println(out);
	}
}
